<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Teacher extends Base
{

	function __construct()
	{
	}

	public function create($param)
	{
		$sid = isset($param['sid']) ? intval($param['sid']):0;
		$name = isset($param['name']) ? trim($param['name']):'';
		$title = isset($param['title']) ? trim($param['title']):'';
		$tel = isset($param['phone']) ? trim($param['phone']):'';
        $ordernum = isset($param['ordernum']) ? intval($param['ordernum']):0;
		$avatar = isset($param['avatar']) ? trim($param['avatar']):'';
		$description = isset($param['description']) ? trim($param['description']):'';
        if (!$sid || !$name || !$title || !$tel) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
    	$store= Db::name('store')->where([['id','=',$sid]])->find();
    	if (!$store) {
    		$this->res_data['Msg'] = "无此门店";
    		return $this->res_data;
    	}
        Db::startTrans();
        try { 
            $user = Db::name('user')->where('status=1 and sid='.$sid.' and phone='.$tel)->find();
            if ($user && ($user['name']!="" || $user['study_status']>1 || $user['openid']!="")) {
                $this->res_data['Msg'] = "该手机号已注册，请核实后再操作";
                return $this->res_data;
            }
            if ($user) {
                $user_data['name'] = $user_data['nickname'] = $name;
                $user_data['sid'] = $sid;
                $user_data['phone'] = $tel;
                $user_data['photo'] = $avatar;
                $user_data['is_teacher'] = 1;
                $user_data['update_time'] = time();
                $is_update = Db::name('user')->where('id',$user['id'])->update($user_data);
                if(!$is_update) {
                    Db::rollBack();
                    $this->res_data['Msg'] = '添加教师失败';
                    return $this->res_data;
                }
                $is_insert = $user['id'];
            } else {
                $user_data['name'] = $user_data['nickname'] = $name;
                $user_data['sid'] = $sid;
                $user_data['phone'] = $tel;
                $user_data['photo'] = $avatar;
                $user_data['is_teacher'] = 1;
                $user_data['create_time'] = time();
                $user_data['update_time'] = time();
                $is_insert = Db::name('user')->insertGetId($user_data);
                if(!$is_insert) {
                    Db::rollBack();
                    $this->res_data['Msg'] = '添加教师失败';
                    return $this->res_data;
                }
            }
            $data['user_id'] = $is_insert;
            $data['sid'] =$sid;
            $data['name'] =$name;
            $data['tel'] =$tel;
            $data['ordernum'] =$ordernum;
            $data['title'] =$title;
            $data['avatar'] =$avatar;
            $data['description'] =$description;
            $data['createtime'] =time();
            $data['updatetime'] =time();
            $data['pwd'] = get_admin_pwd(substr($data['tel'], -6));
            $tdata = Db::name('teacher')->insertGetId($data);
            if(!$tdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '添加教师失败';
                return $this->res_data;
            }
            Db::commit();
            $url = config("http_domain").'dboard/tqr/teacher/tid/'.$tdata;
            $at =file_get_contents($url);
            $at = json_decode($at,true);
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = '添加教师失败';
            return $this->res_data;
        }
	}
	public function edit($param)
	{
		$tid = isset($param['tid']) ? intval($param['tid']):0;
		$name = isset($param['name']) ? trim($param['name']):'';
		$title = isset($param['title']) ? trim($param['title']):'';
		$tel = isset($param['phone']) ? trim($param['phone']):'';
		$avatar = isset($param['avatar']) ? trim($param['avatar']):'';
        $ordernum = isset($param['ordernum']) ? intval($param['ordernum']):0;
		$description = isset($param['description']) ? trim($param['description']):'';
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        if (!$sid || !$tid || !$name || !$title || !$tel ) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
		$teacher = Db::name('teacher')->where([['id','=',$tid]])->find();
		if (!$teacher) {
			$this->res_data['Msg'] = "无此教师";
			return $this->res_data;
		}
        $user = Db::name('user')->where([['id','=',$teacher['user_id']]])->find();
        if (!$user) {
            $this->res_data['Msg'] = "无此用户";
            return $this->res_data;
        }
        if ($tel!=$teacher['tel']) {
            $user = Db::name('user')->where('status=1 and sid='.$sid.' and phone='.$tel)->find();
            if ($user && ($user['name']!="" || $user['study_status']>1 || $user['openid']!="")) {
                $this->res_data['Msg'] = "该手机号已注册，请核实后再操作";
                return $this->res_data;
            }
        }
        Db::startTrans();
        try {
            $data['name'] =$name;
            $data['title'] =$title;
            $data['ordernum'] =$ordernum;
            $data['tel'] =$tel;
            $data['avatar'] =$avatar;
            $data['description'] =$description;
            $data['updatetime'] =time();
            $tdata = Db::name('teacher')->where([['id','=',$tid]])->data($data)->update();
            if(!$tdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '编辑失败';
                return $this->res_data;
            }
            $user_data['name'] = $name;
            $user_data['phone'] = $tel;
            // $user_data['photo'] = $avatar;
            $user_data['update_time'] = time();
            $is_update = Db::name('user')->where([['id','=',$teacher['user_id']]])->data($user_data)->update();
            if(!$is_update) {
                Db::rollBack();
                $this->res_data['Msg'] = '编辑失败';
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
	}
  public function updateTeacher($param)
  {
    $tid = isset($param['tid']) ? intval($param['tid']):0;
    $status = isset($param['status']) ? intval($param['status']):0;
    $teacher= Db::name('teacher')->where([['id','=',$tid]])->find();
    if (!$teacher) {
      $this->res_data['Msg'] = "无此教师";
      return $this->res_data;
    }
    $data['status'] =$status;
    $data['updatetime'] =time();
    $tdata = Db::name('teacher')->where([['id','=',$tid]])->data($data)->update();
    if(!$tdata) {
        $this->res_data['Msg'] = '更新失败';
        return $this->res_data;
    }
    if ($teacher['user_id']) {
        $user = Db::name('user')->where('id',$teacher['user_id'])->find();
        if ($user) {
            Db::name('user')->where('id',$teacher['user_id'])->update(['status'=>0,'update_time'=>time()]);
        }
    }
    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
  }
}

?>
